package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.happyscan138.makefun.BaseAdapter.my_friend;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/6.
 */

public class myFriendAdapter extends BaseAdapter {
    private List<my_friend> mdata;
    private Context mContext;
    private String access_key;
    private String FocusID;
    public myFriendAdapter(Context context,List<my_friend> data, String access_key) {
        this.mdata = data;
        this.mContext = context;
        this.access_key=access_key;
    }





    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        ViewHolder_myFriend viewHolder;
        if(view==null){
            viewHolder=new ViewHolder_myFriend();
            view=View.inflate(mContext, R.layout.my_friend_listview_item,null);
            viewHolder.myFriend_head=(ImageView) view.findViewById(R.id.my_friend_head);
            viewHolder.myFriend_name=(TextView) view.findViewById(R.id.my_friend_name);
            viewHolder.CancelNotice=(Button)view.findViewById(R.id.remove_notice);
//          viewHolder.myFriend_text=(TextView) view.findViewById(R.id.my_friend_text);

            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder=(ViewHolder_myFriend) view.getTag();
        }
        my_friend bean=mdata.get(position);
        FocusID=bean.focus_id;
        //通过viewHolder找到对应控件，避免每次通过findViewById找控件
//        new LoadImagesTask(viewHolder.myFriend_head).execute(bean.head);
        Picasso.with(mContext).load(bean.head).into(viewHolder.myFriend_head);
        viewHolder.myFriend_name.setText(bean.name);
        viewHolder.CancelNotice.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                log.e("onSuccess","onclick");
                client=new AsyncHttpClient();
                params=new RequestParams();
                params.put("access_key",access_key);
                params.put("focus_id",FocusID);
                log.e("parames",params.toString());
                RequestHandle handle=client.post(Urls.CancelNotice,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        log.e("onSuccess",response.toString());
                        String jsonstr=response.toString();
                        try {
                            JSONObject jsonObject=new JSONObject(jsonstr);
                            String msg = jsonObject.getString("msg");
                            Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
                            String code=jsonObject.getString("code");
                            int code_int=Integer.valueOf(code).intValue();
                            if(code_int==1) {
                                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT);
                                notifyDataSetChanged();
                            }else {
                                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                });
            }
        });
//        viewHolder.myFriend_text.setText(bean.text);
        return view;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


}
class ViewHolder_myFriend{
    public ImageView myFriend_head;
    public TextView myFriend_name;
    public Button CancelNotice;
//    public TextView myFriend_text;

}